using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

using Impact.Infrastructure.Plugins;
using Impact.Plugins.net.webservicex.www;
using System.Web.UI.HtmlControls;

namespace Impact.Plugins
{
    /// <summary>
    /// Plugin for display the current weather on an Impact Page
    /// </summary>
    public class CurrentWeatherPlugin : IImpactPlugin
    {
        private static Guid _id = new Guid("76021802-B56A-4507-8A3F-28EF8C147A59");
        private static Dictionary<string, PluginParameter> parameters;

        /// <summary>
        /// Title parameter name
        /// </summary>
        public const string Title = "Title";
        /// <summary>
        /// Zipcode parameter name
        /// </summary>
        public const string Zipcode = "Zipcode";


        #region IImpactPlugin Members

        /// <summary>
        /// unique ID of this plugin
        /// </summary>
        public Guid PluginId
        {
            get { return _id; }
        }

        /// <summary>
        /// friendly string name of this plugin
        /// </summary>
        public string Name
        {
            get { return "Current Weather"; }
        }

        /// <summary>
        /// list of parameters this plugin uses 
        /// </summary>
        public Dictionary<string, PluginParameter> Parameters
        {
            get
            {
                if (parameters == null)
                {
                    parameters = new Dictionary<string, PluginParameter>();
                    parameters.Add(Title, new PluginParameter(this, Title, typeof(string), false));
                    parameters.Add(Zipcode, new PluginParameter(this, Zipcode, typeof(string), true));
                }
                return parameters;
            }
        }

        /// <summary>
        /// no administrative interface used for this plugin 
        /// </summary>
        public IPluginAdmin Admin
        {
            get { return null; }
        }

        /// <summary>
        /// using the given parameter values, renders the plugin as an Html control
        /// </summary>
        public void Render(Control container, Dictionary<string, PluginParameter> parameterValues)
        {
            HtmlGenericControl div = new HtmlGenericControl( "div" );
            div.Attributes.Add("id", "weather");
            container.Controls.Add( div );

            string zipcode = parameterValues[Zipcode].Value;
            if (!string.IsNullOrEmpty(zipcode))
            {

                string weatherReport = GetWeatherReport(zipcode);
                if (!string.IsNullOrEmpty(weatherReport))
                {

                    HtmlGenericControl title = new HtmlGenericControl("p");
                    title.Attributes.Add("class", "title");
                    title.InnerHtml = parameterValues[Title].Value;
                    div.Controls.Add(title);

                    HtmlGenericControl body = new HtmlGenericControl("p");
                    body.Attributes.Add("class", "body");
                    body.InnerHtml = weatherReport;
                    div.Controls.Add(body);

                }

            }
            else
            {
                throw new ArgumentException("Zipcode not configured for Current Weather plugin.");
            }

        }

        /// <summary>
        /// queries the webservice for the weather
        /// </summary>
        private string GetWeatherReport(string zipcode)
        {
            StringBuilder report = new StringBuilder();
            WeatherForecast forecast = new WeatherForecast();
            WeatherForecasts forecasts = forecast.GetWeatherByZipCode(zipcode);


            if (forecasts.Details.Length > 0)
            {
                WeatherData weatherData = forecasts.Details[0];
                report.AppendFormat("<p>Weather for {0}, {1} on {2}</p>", forecasts.PlaceName, forecasts.StateCode, weatherData.Day);
                report.AppendFormat("<p>High Temperature: {0}</p>", weatherData.MaxTemperatureF);
                report.AppendFormat("<p>Low Temperature: {0}</p>", weatherData.MinTemperatureF);
            }
            return report.ToString();
        }

        #endregion


    }
}
